#ifndef __BOLTLOG_BENCH_H__
#define __BOLTLOG_BENCH_H__

#include "../log/blotlog.h"
#include "../log/logger.hpp"
#include <chrono>
#include <thread>
#include <vector>
#include <iostream>
#include <string>

namespace tclog {

/**
 * @brief 性能测试函数
 * @param logger_name 日志器名称
 * @param thread_num 线程数量
 * @param msglen 消息长度
 * @param msg_count 消息总数
 */
void bench(const std::string &logger_name, size_t thread_num, size_t msglen, size_t msg_count) {
    auto logger = loggerManager::getInstance().getLogger(logger_name);
    if (!logger) {
        std::cout << "Logger not found: " << logger_name << std::endl;
        return;
    }
    
    std::string msg(msglen, 'A');
    size_t msg_count_per_thread = msg_count / thread_num;
    std::vector<double> cost_time(thread_num);
    std::vector<std::thread> threads;
    
    std::cout << "输入线程数量: " << thread_num << std::endl;
    std::cout << "输出日志数量: " << msg_count << std::endl;
    std::cout << "输出日志大小: " << msglen * msg_count / 1024 << "KB" << std::endl;
    
    for (size_t i = 0; i < thread_num; i++) {
        threads.emplace_back([&, i]() {
            auto start = std::chrono::high_resolution_clock::now();
            for (size_t j = 0; j < msg_count_per_thread; j++) {
                logger->fatal(msg.c_str());
            }
            auto end = std::chrono::high_resolution_clock::now();
            auto cost = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
            cost_time[i] = cost.count();
            auto avg = msg_count_per_thread / cost_time[i];
            std::cout << "线程" << i << "耗时: " << cost.count() << "s" 
                      << " 平均：" << (size_t)avg << "/s" << std::endl;
        });
    }
    
    for (auto &thr : threads) {
        thr.join();
    }
    
    double max_cost = 0;
    for (auto cost : cost_time) {
        max_cost = max_cost < cost ? cost : max_cost;
    }
    
    std::cout << "总消耗时间: " << max_cost << "s" << std::endl;
    std::cout << "平均每秒输出: " << (size_t)(msg_count / max_cost) << "条日志" << std::endl;
    std::cout << "平均每秒输出: " << (size_t)(msglen * msg_count / max_cost / 1024 / 1024) << "MB" << std::endl;
}

} // namespace tclog

#endif

// ==================== 测试实现 ====================

#include "../log/blotlog.h"
#include "../log/logger.hpp"
#include "../log/sink.hpp"

/**
 * @brief 同步日志性能测试
 * @param thread_count 线程数量
 * @param msg_count 消息总数
 * @param msglen 消息长度
 */
void sync_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen) {
    static int num = 1;
    std::string logger_name = "sync_bench_logger" + std::to_string(num++);
    
    INFO("************************************************");
    INFO("同步日志测试: %zu threads, %zu messages", thread_count, msg_count);
    
    // 创建同步日志器
    auto builder = std::make_shared<tclog::GlobalLoggerBuilder>();
    builder->buildLoggerName(logger_name);
    builder->buildLoggerType(tclog::Logger::Type::LOGGER_SYNC);
    builder->buildFormatter("%m");
    builder->buildSink<tclog::FileSink>("./logs/sync.log");
    builder->build();
    
    tclog::bench(logger_name, thread_count, msglen, msg_count);
    INFO("************************************************");
}

/**
 * @brief 异步日志性能测试
 * @param thread_count 线程数量
 * @param msg_count 消息总数
 * @param msglen 消息长度
 */
void async_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen) {
    static int num = 1;
    std::string logger_name = "async_bench_logger" + std::to_string(num++);
    
    INFO("************************************************");
    INFO("异步日志测试: %zu threads, %zu messages", thread_count, msg_count);
    
    // 创建异步日志器
    auto builder = std::make_shared<tclog::GlobalLoggerBuilder>();
    builder->buildLoggerName(logger_name);
    builder->buildLoggerType(tclog::Logger::Type::LOGGER_ASYNC);
    builder->buildFormatter("%m");
    builder->buildSink<tclog::FileSink>("./logs/async.log");
    builder->build();
    
    tclog::bench(logger_name, thread_count, msglen, msg_count);
    INFO("************************************************");
}

/**
 * @brief 基础性能测试
 */
void bench_test() {
    /*异步日志输出*/
    async_bench_thread_log(1, 1000000, 100);
    async_bench_thread_log(5, 1000000, 100);
    // 同步写日志
    sync_bench_thread_log(1, 1000000, 100);
    sync_bench_thread_log(5, 1000000, 100);
}

int main(int argc, char *argv[]) {
    // 创建logs目录
    system("mkdir -p ./logs");
    
    // 运行基础性能测试
    bench_test();
    
    return 0;
}